package db

import (
	"core/rds"
	"core/utils"
	"fmt"
	"strconv"
)

type (
	PicSyncType string
)

const (
	PicBucket        = "picture"
	PicRankNewLimit  = 100000
	PicRankLoveLimit = 100000

	PicSyncUpdate PicSyncType = "update"
	PicSyncCreate PicSyncType = "create"
	PicSyncDelete PicSyncType = "delete"

	PicFieldID                = "id"                // 字段
	PicFieldOriginName        = "originName"        // 字段
	PicFieldName              = "name"              // 字段
	PicFieldComment           = "comment"           // 字段
	PicFieldAuthorName        = "authorName"        // 字段
	PicFieldAuthorUID         = "authorUID"         // 字段
	PicFieldOnline            = "online"            // 字段
	PicFieldLove              = "love"              // 字段
	PicFieldJigsawName        = "jigsawName"        // 配置字段
	PicFieldDragonBonesName   = "dragonBonesName"   // 配置字段
	PicFieldJigsawSizeId      = "jigsawSizeId"      // 配置字段
	PicFieldJigsawListId      = "jigsawListId"      // 配置字段
	PicFieldListIndexId       = "listIndexId"       // 配置字段
	PicFieldIsRotate          = "isRotate"          // 配置字段
	PicFieldIsShow            = "isShow"            // 配置字段
	PicFieldFixedNum          = "fixedNum"          // 配置字段
	PicFieldStartNum          = "startNum"          // 配置字段
	PicFieldGoldNum           = "goldNum"           // 配置字段
	PicFieldMiniIntro         = "miniIntro"         // 配置字段
	PicFieldWater             = "water"             // 配置字段
	PicFieldMusicName         = "musicName"         // 配置字段
	PicFieldShareImgName      = "shareImgName"      // 配置字段
	PicFieldAnimateFullScreen = "animateFullScreen" // 配置字段
	PicFieldCreateTime        = "createTime"        // 配置字段
	PicFieldAnimate           = "animate"           // 配置字段
)

var (
	PicFieldsAll = []string{
		PicFieldID, PicFieldOriginName, PicFieldName, PicFieldComment, PicFieldAuthorName, PicFieldAuthorUID, PicFieldOnline,
		PicFieldJigsawName, PicFieldDragonBonesName, PicFieldJigsawSizeId, PicFieldJigsawListId, PicFieldListIndexId, PicFieldIsRotate, PicFieldIsShow, PicFieldFixedNum, PicFieldStartNum, PicFieldGoldNum, PicFieldMiniIntro, PicFieldWater, PicFieldMusicName, PicFieldShareImgName, PicFieldAnimateFullScreen, PicFieldCreateTime, PicFieldAnimate,
		PicFieldLove,
	}
	PicFieldsConfig = []string{
		PicFieldID, PicFieldOriginName, PicFieldName, PicFieldComment, PicFieldAuthorName, PicFieldAuthorUID, PicFieldOnline,
		PicFieldJigsawName, PicFieldDragonBonesName, PicFieldJigsawSizeId, PicFieldJigsawListId, PicFieldListIndexId, PicFieldIsRotate, PicFieldIsShow, PicFieldFixedNum, PicFieldStartNum, PicFieldGoldNum, PicFieldMiniIntro, PicFieldWater, PicFieldMusicName, PicFieldShareImgName, PicFieldAnimateFullScreen, PicFieldCreateTime, PicFieldAnimate,
	}
)

// 图片基础key
func MakePicKey(id int64) string {
	return fmt.Sprintf("pic:data:%d", id)
}

// TODO 设计成接口模式
// 排行榜 - 最新
func MakePicRankKeyNew() string {
	return fmt.Sprintf("pic:rank-new")
}

// 排行榜 - 喜欢最多
func MakePicRankKeyLove() string {
	return fmt.Sprintf("pic:data-love")
}

func MakePicRankKeyDay() string {
	return fmt.Sprintf("pic:data-day")
}

func MakePicRankKeyMonth() string {
	return fmt.Sprintf("pic:data-month")
}

// 从redis中获取图片
func LoadPicsConfigByIds(ids []int64) (pics map[int64]*PicOnline) {
	pics = make(map[int64]*PicOnline)
	for _, id := range ids {
		pic, err := LoadPicConfig(id)
		if err != nil {
			utils.Error(err)
			return
		}
		pics[id] = pic
	}
	return
}

// 如果找不到，返回db.ErrNotFound
func LoadPicConfig(id int64) (pic *PicOnline, err error) {
	data, err := rds.GetPicClient().HGetAll(MakePicKey(id)).Result()
	if err != nil {
		return
	}
	if len(data) == 0 {
		return nil, ErrNotFound
	}
	pic = new(PicOnline)
	for k, v := range data {
		switch {
		case k == PicFieldID:
			pic.ID = utils.ToInt64(v)
		case k == PicFieldOriginName:
			pic.OriginName = v
		case k == PicFieldName:
			pic.Name = v
		case k == PicFieldComment:
			pic.Comment = v
		case k == PicFieldAuthorName:
			pic.AuthorName = v
		case k == PicFieldAuthorUID:
			pic.AuthorUID = utils.ToInt64(v)
		case k == PicFieldOnline:
			pic.Online = utils.ToBool(v)
		case k == PicFieldLove:
			pic.Love = utils.ToInt(v)
		case k == PicFieldJigsawName:
			pic.JigsawName = v
		case k == PicFieldDragonBonesName:
			pic.DragonBonesName = v
		case k == PicFieldJigsawSizeId:
			pic.JigsawSizeId = utils.ToInt(v)
		case k == PicFieldJigsawListId:
			pic.JigsawListId = utils.ToInt(v)
		case k == PicFieldListIndexId:
			pic.ListIndexId = utils.ToInt(v)
		case k == PicFieldIsRotate:
			pic.IsRotate = utils.ToBool(v)
		case k == PicFieldIsShow:
			pic.IsShow = utils.ToBool(v)
		case k == PicFieldFixedNum:
			pic.FixedNum = utils.ToInt(v)
		case k == PicFieldStartNum:
			pic.StartNum = utils.ToInt(v)
		case k == PicFieldGoldNum:
			pic.GoldNum = utils.ToInt(v)
		case k == PicFieldMiniIntro:
			pic.MiniIntro = v
		case k == PicFieldWater:
			pic.Water = utils.ToInt(v)
		case k == PicFieldMusicName:
			pic.MusicName = v
		case k == PicFieldShareImgName:
			pic.ShareImgName = v
		case k == PicFieldAnimateFullScreen:
			pic.AnimateFullScreen = v
		case k == PicFieldCreateTime:
			pic.CreateTime = utils.ToInt64(v)
		case k == PicFieldAnimate:
			pic.Animate = v
		}
	}
	return
}

func (pic *PicOnline) Save(fields ...string) (err error) {
	sets := make(map[string]interface{})
	for _, field := range fields {
		switch field {
		case PicFieldID:
			sets[PicFieldID] = pic.ID
		case PicFieldOriginName:
			sets[PicFieldOriginName] = pic.OriginName
		case PicFieldName:
			sets[PicFieldName] = pic.Name
		case PicFieldComment:
			sets[PicFieldComment] = pic.Comment
		case PicFieldAuthorName:
			sets[PicFieldAuthorName] = pic.AuthorName
		case PicFieldAuthorUID:
			sets[PicFieldAuthorUID] = pic.AuthorUID
		case PicFieldOnline:
			sets[PicFieldOnline] = pic.Online
		case PicFieldLove:
			sets[PicFieldLove] = pic.Love
		case PicFieldJigsawName:
			sets[PicFieldJigsawName] = pic.JigsawName
		case PicFieldDragonBonesName:
			sets[PicFieldDragonBonesName] = pic.DragonBonesName
		case PicFieldJigsawSizeId:
			sets[PicFieldJigsawSizeId] = pic.JigsawSizeId
		case PicFieldJigsawListId:
			sets[PicFieldJigsawListId] = pic.JigsawListId
		case PicFieldListIndexId:
			sets[PicFieldListIndexId] = pic.ListIndexId
		case PicFieldIsRotate:
			sets[PicFieldIsRotate] = pic.IsRotate
		case PicFieldIsShow:
			sets[PicFieldIsShow] = pic.IsShow
		case PicFieldFixedNum:
			sets[PicFieldFixedNum] = pic.FixedNum
		case PicFieldStartNum:
			sets[PicFieldStartNum] = pic.StartNum
		case PicFieldGoldNum:
			sets[PicFieldGoldNum] = pic.GoldNum
		case PicFieldMiniIntro:
			sets[PicFieldMiniIntro] = pic.MiniIntro
		case PicFieldWater:
			sets[PicFieldWater] = pic.Water
		case PicFieldMusicName:
			sets[PicFieldMusicName] = pic.MusicName
		case PicFieldShareImgName:
			sets[PicFieldShareImgName] = pic.ShareImgName
		case PicFieldAnimateFullScreen:
			sets[PicFieldAnimateFullScreen] = pic.AnimateFullScreen
		case PicFieldCreateTime:
			sets[PicFieldCreateTime] = pic.CreateTime
		case PicFieldAnimate:
			sets[PicFieldAnimate] = pic.Animate
		}
	}
	key := MakePicKey(pic.ID)
	picClient := rds.GetPicClient()
	err = picClient.HMSet(key, sets).Err()
	if err != nil {
		return
	}
	return
}

// 增加图片属性，例如：点赞数
func IncPicProp(picId int64, prop string, incCount int64) (newCount int64, err error) {
	client := rds.GetPicClient()
	newCount, err = client.HIncrBy(MakePicKey(picId), prop, incCount).Result()
	if err != nil {
		utils.Error(err)
		return
	}

	switch prop {
	case PicFieldLove:
		err = client.UpdateRank(MakePicRankKeyLove(), strconv.FormatInt(picId, 10), newCount, PicRankLoveLimit)
		if err != nil {
			return
		}
	}
	return
}
